package yxy.game.pm2.module.therionHunting;

import org.joda.time.DateTime;
import yxy.apple.util.redisson.RedisException;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.BaseGameRedisLockBean;

import java.util.HashMap;

/**
 * @author wrs
 * @name TherionPlayerModule
 * @description 神兽玩法  玩家信息
 * @since 2022-06-09
 */
public class TherionPlayerModule extends BaseGameRedisLockBean {

    // bean==========================================================================

    private DateTime updateTime; //上一次更新时间，用于刷新本服挑战机会

    //hashtable:::::key:1，2，3分别对应本服，战区，世界 value:挑战次数，初始值为2
    private HashMap<Integer, Integer> challengeNum; //挑战次数

    //外面得key:::::1，2，3分别对应本服，战区，世界
    //里面得hashtable:::::key:playercode value:时间戳 用于判断挑战冷却时间
    private HashMap<Integer, HashMap<String, DateTime>> coolDown;

    //外面得key:::::1，2，3分别对应本服，战区，世界
    //里面得hashtable:::::key:神兽key value:是否订阅神兽复活 1订阅，0不订阅
    private HashMap<Integer, HashMap<Integer, Integer>> isBooked; //是否订阅神兽复活

    //外面得key:::::1，2，3分别对应本服，战区，世界
    //里面得hashtable:::::key:神兽key value:神兽玩法挑战记录
    private HashMap<Integer, HashMap<Integer, TherionChallengeRecord>> challengeRecords;

    public HashMap<Integer, HashMap<String, DateTime>> getCoolDown() {
        return coolDown;
    }

    public void setCoolDown(HashMap<Integer, HashMap<String, DateTime>> coolDown) {
        this.coolDown = coolDown;
    }

    public HashMap<Integer, Integer> getChallengeNum() {
        return challengeNum;
    }

    public void setChallengeNum(HashMap<Integer, Integer> challengeNum) {
        this.challengeNum = challengeNum;
    }

    public DateTime getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(DateTime updateTime) {
        this.updateTime = updateTime;
    }

    public HashMap<Integer, HashMap<Integer, Integer>> getIsBooked() {
        return isBooked;
    }

    public void setIsBooked(HashMap<Integer, HashMap<Integer, Integer>> isBooked) {
        this.isBooked = isBooked;
    }

    public HashMap<Integer, HashMap<Integer, TherionChallengeRecord>> getChallengeRecords() {
        return challengeRecords;
    }

    public void setChallengeRecords(HashMap<Integer, HashMap<Integer, TherionChallengeRecord>> challengeRecords) {
        this.challengeRecords = challengeRecords;
    }

    // func===========================================================================

    private static TherionPlayerModule create(String playerCode) {
        TherionPlayerModule module = new TherionPlayerModule();
        HashMap<Integer, HashMap<String, DateTime>> coolDownTime = new HashMap<>();
        coolDownTime.put(TherionModule.THERION_LOCAL,new HashMap<>()); //本服
        coolDownTime.put(TherionModule.THERION_CROSS,new HashMap<>()); //战区
        coolDownTime.put(TherionModule.THERION_WORLD,new HashMap<>()); //跨服世界

        HashMap<Integer, Integer> map = new TherionHandle().initChallengeNum(new HashMap<>());

        HashMap<Integer, HashMap<Integer, Integer>> bookedMap = new TherionHandle().initIsbooked(new HashMap<>());
        HashMap<Integer, HashMap<Integer, TherionChallengeRecord>> challengeRecordsMap = new TherionHandle().initChallengeRecords(new HashMap<>());

        module.setUpdateTime(DateTime.now());
        module.setCoolDown(coolDownTime); //挑战冷却时间
        module.setChallengeNum(map); //挑战次数
        module.setIsBooked(bookedMap);//神兽复活信息是否订阅
        module.setChallengeRecords(challengeRecordsMap);//神兽玩法挑战记录

        module.set(playerCode);
        return module;
    }

    private void update(String playerCode){
        int lastUpdateTime = getUpdateTime().getDayOfYear();
        //不是同一天，刷新挑战机会、挑战记录
        if (lastUpdateTime != DateTime.now().getDayOfYear()){
            HashMap<Integer,Integer> map = new TherionHandle().initChallengeNum(new HashMap<>());
            setChallengeNum(map); //挑战次数
            setUpdateTime(DateTime.now());
            HashMap<Integer, HashMap<Integer, TherionChallengeRecord>> challengeRecords = new TherionHandle().initChallengeRecords(new HashMap<>());
            setChallengeRecords(challengeRecords);//玩法挑战记录
            set(playerCode);
        }
    }

    // redis==========================================================================


    static public String key(String playerCode) {
        return String.format("PLAYER:%s:MODULE:THERION", playerCode);
    }

    public void set(String playerCode) {
        try {
            setv(key(playerCode),this);
            check();
        }catch (RedisException e){
            e.printStackTrace();
        }
    }

    static public TherionPlayerModule get(String playerCode) {

        try {
            TherionPlayerModule module = getv(key(playerCode));
            //todo 后期放数据库
            if (module == null) {
                module = MySQLCacheDAO.getV(key(playerCode), TherionPlayerModule.class);
                if (module != null) module.set(playerCode);
            }

            if (module == null) {
                module = create(playerCode);
            }
            module.update(playerCode);
            //module.check();
            return module;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }


    //红点
    private void check() {

    }

    //存储到数据库的方法
    public static void saveToDB(String playerCode) {
        try {
            String key = key(playerCode);
            if (touchBucket(key).isExists()) {
                TherionPlayerModule module = TherionPlayerModule.get(playerCode);
                MySQLCacheDAO.setV(key, module);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //从redis移走数据的操作
    public static void removeAllFromRedis(String playerCode) {
        try {
            touchBucket(key(playerCode)).delete();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // lock==========================================================================

    static private String lockKey(String playerCode) {
        return String.format("lock:%s", key(playerCode));
    }

    static public boolean lockTherionPlayerModule(String playerCode) throws InterruptedException {
        return tryLock(lockKey(playerCode));
    }

    static public void unlockTherionPlayerModule(String playerCode) {
        unlock(lockKey(playerCode));
    }

}
